/*
 * mhcontext.h
 *
 *  Created on: 2010-7-14
 *      Author: angelstone
 */

#pragma once

#include "hiberlite.h"
#include "soci.h"

namespace mediahouse {
	class CMHAddon;
	class CMHScannerAddon;
	class CMHMediaAddon;
	class CMHMedia;

	class CMHContext {
	public:
		CMHContext();
		virtual ~CMHContext();

	public:
		bool FindCategoryId(const wxString & name, wxInt32 & id, bool create = true);
		bool FindCategoryFullId(const wxString & path, wxString & full_path, bool create = true);

		bool FindMediaByLocation(const wxString & location,
				hiberlite::bean_ptr<mediahouse::CMHMedia> & media);

		hiberlite::Database * GetDatabase() const { return m_pDatabase; }
		wxString GetDatabaseName() const { return m_DatabaseName; }

		soci::session * GetSqlSession() const { return m_pSqlSession; }

		int ScanItem(const wxString & scanPath, void * cookie);
		int HandleMedia(hiberlite::bean_ptr<mediahouse::CMHMedia> & p_media, void * cookies);

		void NotifyTotalScanItems(int count, void * cookie, const wxString & msg = wxEmptyString);
		void NotifyItemScaned(int count, void * cookie, const wxString & msg = wxEmptyString, bool delta = true);

		bool Cancelled();
		void CancelTask(bool cancel = true);

		wxThread * GetTaskThread() const { return m_pThread; }
		void SetTaskThread(wxThread * pThread) { m_pThread = pThread; }
	protected:
		CMHScannerAddon * FindScanner(const wxString & scanPath);

		void LoadAllAddons();
		void CleanUpAddons();

	private:
		hiberlite::Database * m_pDatabase;
		soci::session * m_pSqlSession;
		void * m_pSavedConn;

		wxString m_DatabaseName;

		WX_DEFINE_ARRAY(CMHAddon *, CMHAddonArray);

		CMHAddonArray m_Addons;

		bool m_cancelled;
	  wxCriticalSection m_csCancelled;        // protects m_cancelled

	  wxThread * m_pThread;
	};
}
